import java.util.Deque;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;

/**
* @description: 841. 钥匙和房间
* @author hewei
* @date 2022/9/1 16:05
* @version 1.0
*/

public class canVisitAllRooms {

    public boolean canVisitAllRooms(List<List<Integer>> rooms) {
        Deque<Integer> queue = new LinkedList<>();
        HashSet<Integer> set = new HashSet<>();
        queue.add(0);
        set.add(0);
        while (!queue.isEmpty()) {
            Integer poll = queue.poll();
            for (Integer a : rooms.get(poll)) {
                if (set.contains(a)) continue;
                set.add(a);
                queue.add(a);
            }
        }
        return set.size() == rooms.size();
    }
}
